package com.example.yrdev.ordering_system.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.example.yrdev.ordering_system.Common.Setting;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;
import com.example.yrdev.ordering_system.http.NetWorkConstant;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by luke on 12/11/16.
 */

public class InternetSettingDialog extends Dialog {

    private Context context;
    @InjectView(R.id.ipAddr)EditText ipAddr;
    @InjectView(R.id.port)EditText port;
    @InjectView(R.id.devNo)EditText devNo;



    public InternetSettingDialog(Context context){
        super(context);
        this.context=context;
        //setTitle("网络设置");

        requestWindowFeature(Window.FEATURE_NO_TITLE);


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.internet_setting_dialog);
        ButterKnife.inject(this);
        getWindow().setLayout(  WindowManager.LayoutParams.MATCH_PARENT,   WindowManager.LayoutParams.WRAP_CONTENT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);//直接弹出输入框\
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        initView();
    }

    private  void  initView(){
        devNo.setText(Setting.getInstance().getDevID());
        ipAddr.setText(NetWorkConstant.defaultIp);
        port.setText(NetWorkConstant.defaultPort);
    }

    @OnClick(R.id.save)
    void saveSetting(){

        if (devNo.length()<=0){
            GlobalFunction.ToastAlert(context,"请输入机号");
        }


        if (port.length()<=0){
            GlobalFunction.ToastAlert(context,"端口不能为空");
        }



        if (NetWorkConstant.isIPAddress(ipAddr.getText().toString())){

            if (port.length()>0 && devNo.length()>0){
String no = devNo.getText().toString();
                Setting.getInstance().setDevID(no);
                Setting.getInstance().saveDevID();
                NetWorkConstant.saveNetwork(context,ipAddr.getText().toString(),port.getText().toString());

                GlobalFunction.ToastAlert(context,"保存成功。");
                dismiss();

            }


        }else {

            GlobalFunction.ToastAlert(context,"请检查您输入的ip地址");
        }

    }

}
